package com.example.schedulecard;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import com.example.schedulecard.DataTransfer.ScheduleNameTransfer;
import com.example.schedulecard.Databases.CourseDatabaseOption;
import com.example.schedulecard.Entity.Course;

public class AddCourses extends AppCompatActivity {
//    定义课程名 教师名 教室名 三个输入框
    EditText inputCourseName;
    EditText inputTeacher;
    EditText inputClassRoom;

    TextView add_name;
//    定义星期 课程开始 课程结束 三个下拉框
    Spinner inputDay;
    Spinner inputStart;
    Spinner inputEnd;
//    新建按钮
    Button addButton, reButton;
    ImageView back;
//    声明数据库
    SQLiteDatabase sqLiteDatabase;

//    String schedulename;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_courses);
        Init();
    }

    private void Init() {
//        定位编辑框
        inputCourseName = (EditText) findViewById(R.id.course_name);
        inputTeacher = (EditText) findViewById(R.id.teacher_name);
        inputClassRoom = (EditText) findViewById(R.id.class_room);
//        定位标题并写入信息
        add_name = (TextView) findViewById(R.id.add_name);
        add_name.setText("添加课程");
//        定位下拉框
        inputDay = (Spinner) findViewById(R.id.week);
        inputStart = (Spinner) findViewById(R.id.classes_begin);
        inputEnd = (Spinner) findViewById(R.id.classes_ends);
//        设置星期下拉框的内容
        ScheduleNameTransfer.setweekdayspinner(this,ScheduleNameTransfer.scheduleinfo,inputDay);
//        打开数据库
        sqLiteDatabase = CourseDatabaseOption.CreateCoursetable(AddCourses.this)
                .getWritableDatabase();
//        定位添加和返回按钮
        addButton = (Button) findViewById(R.id.add_courses_button);
        reButton = (Button) findViewById(R.id.add_re_button);

        back = (ImageView) findViewById(R.id.add_courses_back);
//        Intent intent = getIntent();
//        schedulename = intent.getStringExtra("schedule");
//        为添加按钮添加单击事件
        addButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Course course = new Course(inputCourseName.getText().toString(),
                        inputTeacher.getText().toString(),
                        inputClassRoom.getText().toString(),
                        Integer.valueOf(inputDay.getSelectedItem().toString()),
                        Integer.valueOf(inputStart.getSelectedItem().toString()),
                        Integer.valueOf(inputEnd.getSelectedItem().toString()),
                        ScheduleNameTransfer.scheduleinfo.getSchedulename(),
                        (int) (Math.random() * 100 + 1) % 7
                );
                if (course.getClassRoom().equals("") || course.getTeacher().equals("")
                        || course.getCourseName().equals("")) {
                    Toast.makeText(AddCourses.this, "基本课程信息未填写",
                            Toast.LENGTH_SHORT).show();
                } else {
                    if (course.getClassStart() > course.getClassEnd()) {
                        Toast.makeText(AddCourses.this, "课程开始时间晚于结束时间，请重新选择!",
                                Toast.LENGTH_SHORT).show();
                    } else {
                        if (CourseDatabaseOption.addCoure(sqLiteDatabase, course) > 0) {
                            Toast.makeText(AddCourses.this, "课程信息插入成功!",
                                    Toast.LENGTH_SHORT).show();
                            Intent intenthome = new Intent(AddCourses.this, ScheduleCardHome.class);
                            intenthome.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                            startActivity(intenthome);
                            finish();
                        } else {
                            Toast.makeText(AddCourses.this, "课程信息插入失败!\n" +
                                    "请检查课程开始或结束是否与其他课程重复", Toast.LENGTH_SHORT).show();
                        }
                    }
                }
            }
        });
//        为重置按钮添加单击事件
        reButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                inputCourseName.setText("");
                inputTeacher.setText("");
                inputClassRoom.setText("");
                inputDay.setSelection(0);
                inputStart.setSelection(0);
                inputEnd.setSelection(0);
            }
        });
//        为返回按钮添加单击事件
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
    }
}